package com.sx.zongzhi.voiceamr;

import android.content.Context;
import android.os.CountDownTimer;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sx.zongzhi.R;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.ToastUtils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by admin on 2017/11/10.
 */

public class VoiceAmrMain {
    // 获取类的实例
    MediaReocderOpt recorder;
    //录音地址
    String filePath;
    private ImageView ivToggle;
    private TextView numTime;
    private Context context;
    private int maxTime = 20;//20秒
    private int countdown = 0;//倒计时
    private boolean isStart = false;
    private int type = 1; //1表示发邮箱

    public VoiceAmrMain(final Context context, int types, final ImageView ivToggle, final TextView numTime) {
        this.context = context;
        this.ivToggle = ivToggle;
        this.numTime = numTime;
        this.type = types;
        numTime.setText("··· 00:00 ···");
        String name = BaseUtil.getTime();

//        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath());
//        if (file.exists() && file .isDirectory()){
//            filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath()+ File.separator + name + "media.amr";
//        }else {
            File file1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Voice");
            if (!file1.exists()){
                file1.mkdirs();
            }
            filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Voice"+ File.separator+name +"media.amr";
//        }
        recorder = new MediaReocderOpt();
        recorder.Init(filePath);

        ivToggle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isStart){
                    isStart = false;
                    if (ivToggle != null){
                        ivToggle.setImageResource(R.mipmap.icon_stop);
                    }
                    stopRecord();
                    if (!new File(filePath).exists()) {
                        Toast.makeText(context, "录音失败", Toast.LENGTH_SHORT).show();
                    } else {

                        if (mCallback!=null){
                            mCallback.getData(filePath,countdown);
                        }
//                        if (type == 1){
//                            ((AddMailActivity)context).uploadVoice(filePath,countdown);
//                        }else if(type==2){
//                            ((AddPatrolRecordActivity)context).uploadVoice(filePath,countdown);
//                        }else {
//                            ((AddAlarmActivity)context).uploadVoice(filePath,countdown);
//                        }
                    }
                }else {
                    isStart = true;
                    if (ivToggle != null){
                        ivToggle.setImageResource(R.mipmap.icon_recording);
                    }
                    startRecord();
                }
            }
        });
    }
    private void startRecord() {
        //检查sdcard
        if (!isExitsSdcard()) {
            String needSd = context.getResources().getString(R.string.Send_voice_need_sdcard_support);
            ToastUtils.showToast(context,needSd);
            return ;
        }
        recorder.StartRecording();//开始录制
        countdown = 0;
        cdt.start();
    }

    CountDownTimer cdt = new CountDownTimer(20000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            countdown ++;
            //将秒转换成分秒
            Date dates = new Date(countdown * 1000);
            SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
            String times = sdf.format(dates);
            numTime.setText("··· " + times + " ···");
        }
        @Override
        public void onFinish() {
            isStart = false;
            if (ivToggle != null){
                ivToggle.setImageResource(R.mipmap.icon_stop);
            }
            stopRecord();
            if (!new File(filePath).exists()) {
                Toast.makeText(context, "录音失败", Toast.LENGTH_SHORT).show();
            } else {
//                if (type == 1){
//                    ((AddMailActivity)context).uploadVoice(filePath,countdown);
                if (mCallback!=null){
                    mCallback.getData(filePath,countdown);
                }
//                }else if(type == 2){
//                    ((AddPatrolRecordActivity)context).uploadVoice(filePath,countdown);
//                }else {
//                    ((AddAlarmActivity)context).uploadVoice(filePath,countdown);
//                }
            }
        }
    };

    AudioCallback mCallback;

    /**
     * 定义一个接口
     */
    public interface AudioCallback {
        void getData(String filePath,int countdown);
    }

    /*接口回调*/
    public void getAudioBack(AudioCallback callback) {
        mCallback = callback;
    }


    /**
     * Sdcard是否存在
     */
    public static boolean isExitsSdcard() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }
    private void stopRecord() {
        close();
    }
    private void close() {
        if (recorder != null) {//停止文件写入
            System.out.println("stopRecord");
            recorder.StopRecording();
        }
        cdt.cancel();
    }
}
